import 'package:flutter/material.dart';
import 'package:flutter_hsh/fluttergromore/ads/ad_feed_view.dart';
import 'package:get/get.dart';
import 'phone_boost_logic.dart';

/// 手机加速页面
/// 包含：加速动画页（火箭上升）与完成页（提示100分）
class PhoneBoostPage extends StatefulWidget {
  const PhoneBoostPage({Key? key}) : super(key: key);

  @override
  State<PhoneBoostPage> createState() => _PhoneBoostPageState();
}

class _PhoneBoostPageState extends State<PhoneBoostPage> {
  final PhoneBoostLogic controller = Get.put(PhoneBoostLogic());

  @override
  void initState() {
    super.initState();
    // 页面进入即开始加速流程
    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.startBoost();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      final isBoosting = controller.isBoosting.value;
      final showSuccess = controller.showSuccess.value;
      final titleText = isBoosting ? '手机加速' : '手机加速';

      return Scaffold(
        appBar: AppBar(
          title: Text(titleText),
          centerTitle: true,
          elevation: 0,
          backgroundColor: isBoosting ? const Color(0xFF5E35B1) : Colors.lightBlue,
          foregroundColor: Colors.white,
        ),
        body: () {
          if (isBoosting) return _buildBoostingView(context);
          if (showSuccess) return _buildSuccessView();
          return _buildSuccessView();
        }(),
      );
    });
  }

  /// 加速中的动画页面（参考截图，紫蓝渐变+火箭上升）
  Widget _buildBoostingView(BuildContext context) {
    final height = MediaQuery.of(context).size.height;
    final rocketTravel = height * 0.45 * controller.progress.value; // 火箭位移量

    return Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [Color(0xFF7B1FA2), Color(0xFF3F51B5), Color(0xFF283593)],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      child: Stack(
        children: [
          // 左右两侧的速度线（简单竖条模拟）
          Positioned(
            left: 24,
            top: height * 0.15,
            child: _speedLine(),
          ),
          Positioned(
            left: 60,
            top: height * 0.35,
            child: _speedLine(height: 140),
          ),
          Positioned(
            right: 28,
            top: height * 0.25,
            child: _speedLine(height: 160),
          ),

          // 火箭
          Align(
            alignment: Alignment.bottomCenter,
            child: Transform.translate(
              offset: Offset(0, -rocketTravel),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  // 使用 Material 图标模拟火箭
                  const Icon(
                    Icons.rocket_launch,
                    color: Colors.white,
                    size: 120,
                  ),
                  const SizedBox(height: 4),
                  // 火焰效果（渐变圆形）
                  Container(
                    width: 24,
                    height: 32,
                    decoration: const BoxDecoration(
                      shape: BoxShape.circle,
                      gradient: LinearGradient(
                        colors: [Color(0xFFFFA726), Color(0xFFFF7043)],
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),

          // 底部进度与状态提示
          Positioned(
            bottom: 48,
            left: 0,
            right: 0,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 40),
                  child: LinearProgressIndicator(
                    value: controller.progress.value,
                    backgroundColor: Colors.white.withOpacity(0.2),
                    valueColor: const AlwaysStoppedAnimation<Color>(Colors.white),
                  ),
                ),
                const SizedBox(height: 12),
                Text(
                  controller.statusText.value,
                  style: const TextStyle(color: Colors.white, fontSize: 16),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 加速完成页面（参考截图：勾图标与分数）
  Widget _buildSuccessView() {
    return Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [Color(0xFFB3E5FC), Color(0xFFE1F5FE)],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
      width: double.infinity,
      height: double.infinity,
      child: const Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(Icons.check_circle_outline, color: Colors.green, size: 58),
          SizedBox(height: 16),
          Text('手机状态已无可挑剔', style: TextStyle(fontSize: 18, color: Colors.black87)),
          SizedBox(height: 24),
          Text('100 分', style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold, color: Colors.black87)),
          SizedBox(height: 10),
          MyAdFeedView(),
        ],
      ),
    );
  }

  /// 速度线控件：用来营造加速动感
  Widget _speedLine({double height = 120}) {
    return Container(
      width: 6,
      height: height,
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.8),
        borderRadius: BorderRadius.circular(3),
      ),
    );
  }
}